package com.ruoyi.zhjj.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.web.domain.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.zhjj.domain.ZhjjContractorInfo;
import com.ruoyi.zhjj.service.IZhjjContractorInfoService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.beans.BeanUtils;
import javax.validation.Validator;
import javax.validation.ConstraintViolation;
import java.util.Set;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 承包商信息Controller
 *
 * @author ruoyi
 * @date 2024-12-23
 */
@RestController
@RequestMapping("/contractor")
public class ZhjjContractorInfoController extends BaseController
{
    @Autowired
    private IZhjjContractorInfoService zhjjContractorInfoService;


    /**
     * 查询承包商信息列表
     */
    @RequiresPermissions("zhjj:ZhjjContractorInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(ZhjjContractorInfo zhjjContractorInfo)
    {
        startPage();
        List<ZhjjContractorInfo> list = zhjjContractorInfoService.selectZhjjContractorInfoList(zhjjContractorInfo);
        return getDataTable(list);
    }

    /**
     * 导出承包商信息列表
     */
    @RequiresPermissions("zhjj:ZhjjContractorInfo:export")
    @Log(title = "承包商信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZhjjContractorInfo zhjjContractorInfo)
    {
        List<ZhjjContractorInfo> list = zhjjContractorInfoService.selectZhjjContractorInfoList(zhjjContractorInfo);
        ExcelUtil<ZhjjContractorInfo> util = new ExcelUtil<ZhjjContractorInfo>(ZhjjContractorInfo.class);
        util.exportExcel(response, list, "承包商信息数据");
    }

    /**
     * 获取承包商信息详情信息
     */
    @RequiresPermissions("zhjj:ZhjjContractorInfo:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(zhjjContractorInfoService.selectZhjjContractorInfoById(id));
    }

    /**
     * 新增承包商信息
     */
    @RequiresPermissions("zhjj:ZhjjContractorInfo:add")
    @Log(title = "承包商信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated ZhjjContractorInfo zhjjContractorInfo)
    {

        return toAjax(zhjjContractorInfoService.insertZhjjContractorInfo(zhjjContractorInfo));
    }

    /**
     * 修改承包商信息
     */
    @RequiresPermissions("zhjj:ZhjjContractorInfo:edit")
    @Log(title = "承包商信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated ZhjjContractorInfo zhjjContractorInfo)
    {
        return toAjax(zhjjContractorInfoService.updateZhjjContractorInfo(zhjjContractorInfo));
    }

    /**
     * 删除承包商信息
     */
    @RequiresPermissions("zhjj:ZhjjContractorInfo:remove")
    @Log(title = "承包商信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(zhjjContractorInfoService.deleteZhjjContractorInfoByIds(ids));
    }


/*    @PostMapping("/api/site/external/server/contractor/info/handle")
    public ApiResponse addZhjjContractorInfo(@RequestBody Map<String, Object> requestMap) throws IOException {
        // 获取操作类型和数据
        String operateFlag = (String) requestMap.get("operateFlag");
        Map<String, Object> data = (Map<String, Object>) requestMap.get("data");

        // 基础参数验证
        if (data == null) {
            return ApiResponse.error("510", "数据不能为空");
        }
        // 使用 ObjectMapper 进行转换
        ZhjjContractorInfo contractorInfo = objectMapper.convertValue(data, ZhjjContractorInfo.class);
        // 使用Validator进行验证
        Set<ConstraintViolation<ZhjjContractorInfo>> violations = validator.validate(contractorInfo);
        if (!violations.isEmpty()) {
            // 获取第一个错误信息
            String errorMessage = violations.iterator().next().getMessage();
            return ApiResponse.error("510", errorMessage);
        }
        // 重复数据验证
        if ("create".equals(operateFlag)) {
            ZhjjContractorInfo existingInfo = zhjjContractorInfoService.selectZhjjContractorInfoById(Long.valueOf(contractorInfo.getExtSysId()));
            if (existingInfo != null) {
                return ApiResponse.error("533", "源数据表ID已存在");
            }
        }

        try {
            // 根据操作类型执行相应的操作
            int result = 0;
            String message = "";
            String extSysId = contractorInfo.getExtSysId();

            switch (operateFlag) {
                case "create":
                    result = zhjjContractorInfoService.insertZhjjContractorInfo(contractorInfo);
                    message = "新增成功";
                    break;
                case "edit":
                    result = zhjjContractorInfoService.updateZhjjContractorInfo(contractorInfo);
                    message = "修改成功";
                    break;
                case "del":
                    result = zhjjContractorInfoService.deleteZhjjContractorInfoById(Long.valueOf(extSysId));
                    message = "删除成功";
                    break;
                default:
                    return ApiResponse.error("500", "无效的操作类型");
            }

            if (result>0) {
                return ApiResponse.success(message, extSysId);
            } else {
                return ApiResponse.error("500", operateFlag.equals("create") ? "保存失败" :
                                              operateFlag.equals("edit") ? "修改失败" : "删除失败");
            }
        } catch (Exception e) {
            return ApiResponse.error("500", "操作失败：" + e.getMessage());
        }
    }*/
}
